package com.cyf.plc.ui.fragment.set

import android.graphics.drawable.Drawable
import android.view.View
import android.widget.EditText
import androidx.core.content.ContextCompat
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import com.afollestad.materialdialogs.MaterialDialog
import com.afollestad.materialdialogs.callbacks.onPreShow
import com.afollestad.materialdialogs.customview.customView
import com.afollestad.materialdialogs.customview.getCustomView
import com.afollestad.materialdialogs.lifecycle.lifecycleOwner
import com.cyf.plc.R
import com.cyf.plc.adapter.SubAttributeAdapter
import com.cyf.plc.base.BaseFragment
import com.cyf.plc.databinding.FragmentAttributeBinding
import com.cyf.plc.model.PlcSubAttribute
import com.cyf.plc.util.viewModelOf
import com.cyf.plc.viewmodel.PlcViewModel
import kotlinx.coroutines.ExperimentalCoroutinesApi

@ExperimentalCoroutinesApi
class SubAttributeFragment : BaseFragment<PlcViewModel, FragmentAttributeBinding>(),
    SubAttributeAdapter.OnItemClickListener {

    private val mAdapterSub: SubAttributeAdapter by lazy { SubAttributeAdapter(onItemClickListener = this) }

    override fun createViewModel(): PlcViewModel = viewModelOf(mViewModelProvider)

    override fun initView() {

        val divider = DividerItemDecoration(this.context, DividerItemDecoration.VERTICAL)

        val drawable: Drawable = this.context?.let {
            ContextCompat.getDrawable(
                it,
                R.drawable.drawable_custom_divider
            )
        }!!
        drawable.setBounds(0, 0, 4, 0)

        divider.setDrawable(drawable)

        // Initialize RecyclerView
        mViewBinding.rvAttribute.apply {
            layoutManager = LinearLayoutManager(this.context)
            adapter = mAdapterSub
            addItemDecoration(divider)
        }
        //
        mViewModel.getPlcSubAttributes().observe(viewLifecycleOwner, Observer {
            if (it != null) {
                mAdapterSub.submitList(it.toMutableList())
            }
        })
    }

    override fun layoutId(): Int = R.layout.fragment_attribute

    override fun onItemClicked(plcSubAttribute: PlcSubAttribute, view: View) {
        //点击设置属性值
        this.context?.let {
            MaterialDialog(it).show {
                onPreShow { dialog ->
                    val etAttributeValue: EditText = dialog.getCustomView().findViewById(R.id.et_attribute_value)
                    etAttributeValue.setText(plcSubAttribute.attributeValue.toString())
                }
                setTitle("设置")
                customView(R.layout.dialog_attribute)
                positiveButton(R.string.login) { dialog ->
                    // Pull the password out of the custom view when the positive button is pressed
                    val etAttributeValue: EditText =
                        dialog.getCustomView().findViewById(R.id.et_attribute_value)
                    plcSubAttribute.attributeValue = etAttributeValue.text.toString().toFloat()
                    mViewModel.savePlcSubAttribute(plcSubAttribute)
                }
                negativeButton(android.R.string.cancel)
                lifecycleOwner(this@SubAttributeFragment)
            }
        }
    }

}